<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;


use think\Model;

class ForumNoteModel extends Model
{
    // 确定链接表名
    protected $name = 'forum_note';
    //开启时间戳写入
    protected $autoWriteTimestamp = true;

    /**
     * 插入新的帖子
     */
    public function insertNote($param)
    {
        try{
            $result =  $this->allowField(true)->validate('ForumNoteValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, '/index/forum/index', '发表帖子成功!');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }


    /**
     * 编辑帖子
     */
    public function updateNote($param){
        try{
            $result =  $this->allowField(true)->validate('ForumNoteValidate')->update($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, '/index/forum/index', '编辑帖子成功!');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除帖子
     */
    public function delNote($params){
        if( !empty($params['id']) && $this->where('id',$params['id'])->delete() ){
            return fmsg(0, '', '删除成功');
        }else{
            return fmsg(1, '', '删除成功');
        }
    }

    /**
     * 设置帖子（置顶、加精）
     */
    public function setNote($params){
        if( isset($params['rank']) && !empty($params['id']) && !empty($params['field']) && ($params['field']=='stick' || $params['field'] =='status') ){
            $field = ($params['field'] == 'stick')?'is_top':'is_good';
            $rank = ($params['rank'] == 1)?1:0;
            if( $this->where('id',$params['id'])->setField($field,$rank) ){
                return fmsg(0, '', '设置成功');
            }else{
                return fmsg(1, '', '设置失败');
            }
        }else{
            return fmsg(1, '', '设置失败');
        }

    }

    /**
     * 获取帖子列表
     * @return mixed
     */
    public function getNoteList($where)
    {
        return $this->where($where)->order("update_time desc")->select();
    }

    /**
     * 获取置顶帖子数据
     * @return mixed
     */
    public function getTopNoteList()
    {
        $top_list = $this->alias('a')
            ->join('z_forum_note_cate b','a.cate_id=b.id','LEFT')
            ->join('z_center c','a.center_id=c.id','LEFT')
            ->where(['a.state'=>1,'a.is_top'=>1])
            ->order("a.update_time desc")
            ->field('a.id,a.center_id,a.cate_id,a.title,a.credit,a.update_time,a.comment_num,a.is_end,a.is_good,a.is_top,b.name,c.username,c.disabled,c.thumb')
            ->select();
        return $top_list;
    }

    /**
     * 获取帖子数量
     * @param $where
     * @return int|string
     */
    public function getNoteCount($where)
    {
        return $this->where($where)->count();
    }


    /**
     * 获取一条帖子
     * @param $where
     * @return int|string
     */
    public function getOneNote($params)
    {
//        return $this->where($where)->find();
        return  $this->alias('a')
        ->join('z_forum_note_cate b','a.cate_id=b.id','LEFT')
        ->join('z_center c','a.center_id=c.id','LEFT')
        ->where(['a.id'=>$params['id'] ])
        ->order("a.update_time desc")
        ->field('a.id,a.center_id,a.cate_id,a.title,a.content,a.credit,a.update_time,a.comment_num,a.is_end,a.is_good,a.is_top,b.name,c.username,c.disabled,c.thumb')
        ->find();
    }

    /**
     *修改帖子评论数
     */
    public function setCommentNum($id,$oprate = '+')
    {
//        dd(11,!empty($id));
        if(!empty($id) ){
            if( $oprate == '+' && $this->where('id',$id)->setInc('comment_num') ||
                $oprate == '-' && $this->where('id',$id)->setDec('comment_num') ) {
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }




}